/*
 * Copyright (c) 2019.  鼎捷软件股份有限公司.All rights reserved.
 */

package com.digiwin.dap.middleware.util;

import com.digiwin.dap.middleware.exception.OperateException;
import org.apache.tomcat.util.codec.binary.Base64;

import java.security.MessageDigest;

public final class AESCipher {

    public static String getSHA256(String str) {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            byte[] bytes = messageDigest.digest();
            messageDigest.update(bytes);
            encodeStr = Base64.encodeBase64String(messageDigest.digest());
        } catch (Exception e) {
            throw new OperateException(e);
        }
        return encodeStr;
    }

    /**
     * C#版本策略ID(policy)加密模式
     *
     * @param str
     * @return
     */
    public static String encode(String str) {
        String encodeStr = "";
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] bytes = messageDigest.digest(str.getBytes("UTF-8"));
            bytes = messageDigest.digest(bytes);
            encodeStr = Base58Encoding.encode(bytes).substring(0, 20);
        } catch (Exception e) {
            throw new OperateException(e);
        }
        return encodeStr;
    }

}